package com.hxd.otc.module.order.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RadioGroup;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.module.order.fragment.AdOrderFragment;
import com.hxd.otc.module.order.fragment.OrderFragment;
import com.hxd.otc.mvp.base.BasePresenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by lzy on 2018/7/9 0009.
 * description ${订单页面}
 */
public class OrderActivity1 extends BaseActivity {

    @BindView(R.id.radio_group)
    RadioGroup radio_group;
    private FragmentManager fragmentManager;

    OrderFragment normalFra;
    AdOrderFragment adFra;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_order;
    }


    @Override
    protected void initView() {
        radio_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_normal:
                        setTabSelection(0);
                        break;
                    case R.id.rb_ad:
                        setTabSelection(1);
                        break;
                }
            }
        });
    }

    private void hideFragments(FragmentTransaction transaction) {

        if (normalFra != null) {
            transaction.hide(normalFra);
        }
        if (adFra != null) {
            transaction.hide(adFra);
        }

    }

    private void setTabSelection(int position) {
        final FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragments(transaction);
        switch (position) {
            case 0:
                if (normalFra == null) {
                    normalFra = new OrderFragment();
                    Bundle bundle = new Bundle();
                    normalFra.setArguments(bundle);
                    transaction.add(R.id.container, normalFra);
                } else {
                    transaction.show(normalFra);
                }
                break;
            case 1:
                if (adFra == null) {
                    adFra = new AdOrderFragment();
                    Bundle bundle = new Bundle();
                    adFra.setArguments(bundle);
                    transaction.add(R.id.container, adFra);
                } else {
                    transaction.show(adFra);
                }
                break;
        }
        transaction.commit();
    }

    @OnClick({R.id.iv_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    protected void initData() {
        fragmentManager = getSupportFragmentManager();
        setTabSelection(0);
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }


}
